<?php
namespace app\index\controller;

use think\Controller;
use app\common\controller\Base;
use think\Session;
class User extends Base{

    public function index()
    {
        return 'index/index';
    }
    public function login(){


      return $this->fetch();
    }
    



    public function logincheck(){
        //判定
        if(!request()->isPost()) {
            $this->error('提交不合法');
        }
        $data = input('post.');
//        dump($data);

        //严格检验 tp5 validate
        if(!captcha_check($data['verify'])){
            //验证失败
            $this->error('验证码不正确');
        }
        try {
            $user = model('User')->getUserByUsername($data['name']);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        ///print_r($user);

        if(!$user || $user->status != 1) {
            $this->error('该用户不存在');
        }
        // 判定密码是否合理

        if(md5($data['password'].$user->code) != $user->password) {
//            echo md5($data['password'].$user->code).'<br>';
//            echo $user->password;
            $this->error('密码不正确');
        }

        // 登录成功
//        model('User')->updateById(['last_login_time'=>time()], $user->id);

        //把用户信息记录到session
        session('user', $user);

        $this->success('登录成功', url('index/index'));
    }

    public function register(){
        if(request()->isPost()){
            $data = input('post.');

//            if(!captcha_check($data['verifycode'])) {
//                // 校验失败
//                $this->error('验证码不正确');
//            }
            //严格校验 tp5 validate

            if($data['password'] != $data['repassword']) {
                $this->error('两次输入的密码不一样');
            }
            // 自动生成 密码的加盐字符串
            $data['code'] = mt_rand(100, 10000);
            $data['password'] = md5($data['password'].$data['code']);
            //$data = 12;// test
          
            $user = model('User')->getUserByUsername($data['name']);
            if($user){
                      $this->error('该用户名已存在');
            }
        
            try {
                $res = model('User')->add($data);
            }catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            if($res) {
                $this->success('注册成功',url('user/login'));
            }else{
                $this->error('注册失败');
            }

        }else {
            return $this->fetch();
        }
        //QLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'singwa' for key 'username'


    
    }
    public function message(){

        if(request()->isPost()) {
            $data = input('post.');
            $validate = new \app\common\validate\Message();
            $result = $validate->check($data);
            if(!$result){
                $this->error($validate->getError()) ;
            }
            try {
                $res = model('Message')->add($data);
            }catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            if($res) {
                $this->success('留言成功',url('user/message'));
            }else{
                $this->error('留言失败');
            }
        
        }
        return $this->fetch();
    }
    function clears(){
        Session::clear();
        $this->success('退出成功');
    }
}
